package com.conv.shop.business.controller;

import com.conv.shop.business.resp.StoreUserResp;
import com.conv.shop.business.service.StoreService;
import com.conv.shop.business.util.JwtUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/store")
public class StoreManageController {
    @Resource
    StoreService storeService;
    @GetMapping("/add")
    public String storeAdd(){
        return "Store,Hello SpringBoot";
    }
    @GetMapping("/update")
    public String storeUpdate(){
        return "Store,Hello SpringBoot";
    }
    @GetMapping("/deleted")
    public String storeDeleted(){
        return "Store,Hello SpringBoot";
    }

    /**
     * 店铺人员查询
     * @param request
     * @return List<StoreUserResp>
     */
    @GetMapping("/membersList")
    public ResponseEntity<List<StoreUserResp>> membersList(HttpServletRequest request){
        String token = request.getHeader("token");
        if (token == null){
            return null;
        }
        if(!JwtUtil.validate(token)){
            return null;
        }
        int consumerId = Integer.parseInt(JwtUtil.getUserNameByToken(token));
        if (storeService.getStoreUserList(consumerId).isEmpty()){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok().body(storeService.getStoreUserList(consumerId));
    }
}
